package com.cy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/7/30 14:43
 */
@SpringBootApplication
public class ScaProviderApplication {

    private static final Logger logger = LoggerFactory.getLogger(ScaProviderApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ScaProviderApplication.class,args);
    }

    // @RefreshScope的作用是在配置中心的相关配置发生变化以后，
    // 能够及时看到更新(底层是通过重新创建Controller对象的方式,对属性进行了重新初始化)
    @RefreshScope //动态刷新配置
    @RestController
    public class providerController{

        @Value("${server.port}")
        private String server;

        @Value("${logging.level.com.cy: info}")
        private String logLevel;

        public providerController(){
            // 底层通过重新创建controller对象,对属性进行初始化
            System.out.println("providerController()"); //
        }

        @GetMapping("/provider/logLevel")
        public String doGetLevel(){
            logger.trace("==log.trace==");//跟踪
            logger.debug("==log.debug==");//调试
            logger.info("==log.info==");//常规信息
            logger.warn("==log.warn==");//警告
            logger.error("==log.error==");//错误信息
            return "log level is " + logLevel;
        }

        @GetMapping("/provider/echo/{msg}")
        public String doEcho(@PathVariable String msg) throws InterruptedException {
            // 模拟超时异常
//            Thread.sleep(500000);
            if (msg == null || msg.length()<3)
                throw new IllegalArgumentException("参数不合法");
            return server + " say: Hello Nacos Discovery " + msg;
        }
    }

}
